<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">销售订单新建</p>
    </div>
    <div class="view-container">
      <el-row class="status-title">
        <el-col class="mr20 width100">
          <el-button
            class="btn-success width100"
            type="primary"
            @click="submitForm(1)"
          >保存</el-button>
        </el-col>
        <el-col class="mr20 width100">
          <el-button
            class="btn-success width100"
            type="primary"
            @click="submitForm(2)"
          >提交</el-button>
        </el-col>
      </el-row>
      <el-form ref="form" :model="form" label-width="140px" class="mt30">
        <el-row>
          <el-col :span="8">
            <el-form-item label="订单编码:" prop="orderCode">
              <el-input v-model="form.orderCode" placeholder="系统自动生成" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="业务日期:" prop="businessDate">
              <el-input v-model="form.businessDate" placeholder="系统自动生成" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="订单类型:" prop="orderType">
              {{ form.orderType }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="客户类型:" prop="customerType">
              <el-select v-model="form.customerType" placeholder="请选择" style="width:100%;" @change="onCustomerType">
                <el-option label="企业客户" value="1">企业客户</el-option>
                <el-option label="会员客户" value="2">会员客户</el-option>
                <!-- <el-option label="内部客户" value="3">内部客户</el-option> -->
                <!-- <el-option label="经销商" value="5">经销商</el-option> -->
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户名称:" prop="customerName">
              <template v-if="form.customerType !== '5'">
                <template v-if="isPlatForm == true">
                  <!-- <el-input v-model="form.customerName" placeholder="请选择客户" @focus="onCustomerShow"> -->
                  <el-input v-model="form.customerName" placeholder="请输入" @input="onInputCustomer" class="customerInput">
                    <el-button slot="append" icon="el-icon-search" @click="onCustomerShow"></el-button>
                  </el-input>
                  <div class="searchItem" v-if="searchCustomerShow">
                    <searchTable :data="searchCustomerData" :columns="searchCustomerColumns" @searchData="onSearchData"></searchTable>
                    <pagination
                      class="searchTable"
                      v-show="searchCustomerTotal > 0"
                      :total="searchCustomerTotal"
                      :page.sync="searchTableQueryParams.pageNum"
                      :limit.sync="searchTableQueryParams.pageSize"
                      @pagination="onInputCustomer"
                      layout="prev, pager, next"
                    />
                  </div>
                </template>
                <template v-else>
                  <el-select v-model="form.customerId" placeholder="请选择" style="width:100%;" @change="onPlatForm">
                    <el-option v-for="(item,index) of customerDeptList" :key="index" :label="item.orgName" :value="item.id" :disabled="item.disabled"></el-option>
                  </el-select>
                </template>
              </template>
              <template v-else>
                <el-select v-model="form.customerId" placeholder="请选择" style="width:100%;" @change="onDealerBinding">
                  <el-option v-for="(item,index) of dealerBindingList" :key="index" :label="item.corporateName" :value="item.id"></el-option>
                </el-select>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="isCustomerCompanyList == true">
            <el-form-item label="客户部门:" prop="customerDept">
              <el-select v-model="form.customerDept" placeholder="请选择" style="width:100%;">
                <el-option v-for="(item,index) of customerCompanyList" :key="index" :label="item.deptName" :value="item.deptName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="发货日期:" prop="shipDate">
              <el-date-picker value-format="yyyy-MM-dd" v-model="form.shipDate" suffix-icon="el-icon-date" type="date" placeholder="请选择发货日期" :picker-options="pickerOptions" style="width:100%;">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人:" prop="contactName">
              <el-input v-model="form.contactName" placeholder="联系人" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系电话:" prop="contactTel">
              <el-input v-model="form.contactTel" placeholder="联系电话" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="收货人:" prop="receiverName">
              <el-input v-model="form.receiverName" placeholder="收货人" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="收货地址:" prop="receiverAddress">
              <el-input v-model="form.receiverAddress" placeholder="收货地址" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="销售部门:" prop="salesDept">
              <el-select v-model="form.salesDept" style="width:100%;" placeholder="请选择">
                <el-option
                    v-for="item in salesDeptList"
                    :key="item.deptId"
                    :label="item.deptName"
                    :value="item.deptId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="发票类型:" prop="invoiceType">
              <el-select v-model="form.invoiceType" style="width:100%;" placeholder="请选择">
                <el-option
                  v-for="item in invoiceTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="上传附件:">
              <el-upload
                action="#"
                :on-remove="handleRemove"
                :before-remove="beforeRemove"
                :on-change="onUploadType"
                :auto-upload="false"
                multiple
                :limit="3"
                :on-exceed="onExceed"
                :file-list="fileList"
                ref="fileUpload"
              >
                <el-button size="small" type="primary">点击上传</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="备注:" prop="remark">
              <el-input v-model="form.remark" placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-table :data="productData">
              <el-table-column label="序号" type="index" align="center" width="50" />
              <el-table-column :label="item.label" :prop="item.prop" :width="item.width" v-for="(item, index) in columns" :key="index" align="center">
                <template slot-scope="scope">
                  <template v-if="item.type == '1'">
                    <el-input v-model="scope.row[item.prop]" placeholder="请输入" @input="onInputProduct(scope.$index,scope.row)" class="productInput">
                      <el-button slot="append" icon="el-icon-search" @click="productShow"></el-button>
                    </el-input>
                  </template>
                  <template v-if="item.type == '3'">{{ scope.row[item.prop] }}</template>
                  <template v-if="item.type == 'num'">
                    <template v-if="form.customerType !== '5'">
                      <el-input placeholder="请输入数量" v-model="scope.row[item.prop]" @input="(val) => inputNum(val, scope.row)" @blur="(val) => inputBlur(val, scope.row)" />
                    </template>
                    <template v-else>
                      <span>{{ scope.row[item.prop] }}</span>
                    </template>
                  </template>
                  <template v-if="item.type == 'unitPrice'">
                    <el-input placeholder="请输入未税单价" v-model="scope.row[item.prop]" @input="(val) => inputUnitPrice(val, scope.row)" v-if="scope.row.priceType == '0'" />
                    <span v-else>{{ scope.row[item.prop] }}</span>
                  </template>
                  <template v-if="item.type == 'taxUnitPrice'">
                    <el-input placeholder="请输入含税单价" v-model="scope.row[item.prop]" @input="(val) => inputTaxUnitPrice(val, scope.row)" v-if="scope.row.priceType == '1'" />
                    <span v-else>{{ scope.row[item.prop] }}</span>
                  </template>
                  <template v-if="item.type == 'taxRate'">
                    <el-input placeholder="请输入税率" v-model="scope.row[item.prop]" @input="inputTaxRate(scope.row)" />
                  </template>
                   <template v-if="item.type == 'batch'">
                     <span >{{ scope.row[item.prop] }}</span>
                  </template>
                  <template v-if="item.type == '5'">
                    <el-input placeholder="备注" v-model="scope.row[item.prop]" />
                  </template>
                  <span class="del" v-if="item.type == 'del'" @click="deleteRow(scope.$index, scope.row)">删除</span>
                </template>
              </el-table-column>
            </el-table>
            <div class="searchProduct" v-if="searchProductShow">
              <searchTable :data="searchProductData" :columns="searchProductColumns" @searchData="onSearchProductData"></searchTable>
              <pagination
                class="searchTable"
                v-show="searchProductTotal > 0"
                :total="searchProductTotal"
                :page.sync="searchTableProductQueryParams.pageNum"
                :limit.sync="searchTableProductQueryParams.pageSize"
                @pagination="onPageProduct"
                layout="prev, pager, next"
                :pager-count="5"
              />
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 弹出数据 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" top="50px" append-to-body center :before-close="handleClose">
      <template v-if="type == '1'">
        <el-form :model="queryParams" ref="queryForm" :inline="true" @submit.native.prevent>
          <el-form-item :label="label" prop="searchValue">
            <el-input
              v-model="queryParams.searchValue"
              placeholder="请输入名称、编码检索"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
          </el-form-item>
        </el-form>
      </template>
      <template v-if="type == '2'">
        <el-form :model="queryParams" ref="queryForm" :inline="true" @submit.native.prevent>
          <el-form-item :label="label" prop="searchValue">
            <el-input
              style="width:250px;"
              v-model="queryParams.searchValue"
              placeholder="请输入名称、编码、批次检索"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
          </el-form-item>
        </el-form>
      </template>
      <template v-if="type == '3'">
        <el-form :model="queryParams" ref="queryForm" :inline="true" @submit.native.prevent>
          <el-form-item :label="label" prop="phone">
            <el-input
              v-model="queryParams.phone"
              placeholder="会员手机号检索"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
          </el-form-item>
        </el-form>
      </template>
      <el-table v-loading="loading" :data="dataList" @row-click="onSelectData" :default-sort="{prop: 'date', order: 'descending'}" border>
        <el-table-column label="序号" type="index" align="center" width="50">
          <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <template v-if="type == '1'">
          <el-table-column label="客户编码" align="center" prop="code" :show-overflow-tooltip="true" width="150" sortable />
            <el-table-column label="客户名称" align="center" prop="name" :show-overflow-tooltip="true" />
            <el-table-column label="联系人" align="center" prop="contact" :show-overflow-tooltip="true" />
            <el-table-column label="联系地址" align="center" :show-overflow-tooltip="true" width="300">
              <template slot-scope="scope">
                {{ scope.row.province + scope.row.city + scope.row.region + scope.row.shippingAddress }}
              </template>
            </el-table-column>
            <el-table-column label="联系方式" align="center" prop="contactNumber" :show-overflow-tooltip="true" />
            <el-table-column label="审核状态" align="center" prop="approvalStatus">
              <template slot-scope="scope">
                <div v-if="scope.row.approvalStatus == 0" style="color:#13C2C2">草稿</div>
                <div v-if="scope.row.approvalStatus == 1" style="color:#13C2C2">待审核</div>
                <div v-if="scope.row.approvalStatus == 2" style="color:#52C41A">已审核</div>
                <div v-if="scope.row.approvalStatus == 3" style="color:#fd562c">拒绝</div>
              </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status">
              <template slot-scope="scope">
                <div v-if="scope.row.status == 0" style="color:#13C2C2">禁用</div>
                <div v-if="scope.row.status == 1" style="color:#52C41A">正常</div>
              </template>
            </el-table-column>
        </template>
        <template v-if="type == '2'">
          <el-table-column label="产品名称" align="center" prop="productName" :show-overflow-tooltip="true" />
          <el-table-column label="SKU编码" align="center" prop="specificationCode" :show-overflow-tooltip="true" sortable />
          <el-table-column label="SKU名称" align="center" prop="specificationName" :show-overflow-tooltip="true" />
          <el-table-column label="规格" align="center" :show-overflow-tooltip="true" width="120">
            <template slot-scope="scope">
              {{ scope.row.value1 == '' || scope.row.value1 == null ? '' : scope.row.value1 + scope.row.value2 }}
            </template>
          </el-table-column>
          <el-table-column label="批次" align="center" prop="batchNumber" :show-overflow-tooltip="true" width="150" />
          <el-table-column label="库存" align="center" prop="storeNumber" :show-overflow-tooltip="true" width="100" />
          <el-table-column label="单位" align="center" prop="unit" :show-overflow-tooltip="true" />
          <el-table-column label="是否启用扫码" align="center" prop="isSerial" width="100">
            <template slot-scope="scope">
              <div v-if="scope.row.isSerial == '0'" style="color:#13C2C2">启用</div>
              <div v-if="scope.row.isSerial == '1'" style="color:#52C41A">禁用</div>
            </template>
          </el-table-column>
        </template>
        <template v-if="type == '3'">
          <el-table-column label="手机号"  align="center" prop="phone" />
          <el-table-column label="姓名" align="center" prop="nickName" />
          <el-table-column label="性别" align="center" prop="sex" />
          <el-table-column label="生日" align="center" prop="dateOfBirth" />
          <el-table-column label="学历" align="center" prop="education" />
          <el-table-column label="备注" align="center" prop="remarks" />
          <el-table-column label="最近一次登录" align="center" prop="lastLoginTime" />
          <el-table-column label="状态" align="center" prop="status">
              <template slot-scope="scope">
                  <div v-if="scope.row.status == '1'">待审核</div>
                  <div v-if="scope.row.status == '2'">已审核</div>
                  <div v-if="scope.row.status == '3'">拒绝</div>
              </template>
          </el-table-column>
        </template>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>
  </div>
</template>

<script>
import { orderAdd, orderEdit, orderDetail, delProduct, deptTree } from "@/api/system/order/order"
import { listUser, detailUser } from "@/api/system/basedata/user/user"
import { listSalesProduct } from "@/api/system/basicdata/basicdata"
import { memberList, memberDetail } from "@/api/system/member/member"
import { list } from "@/api/system/dealerBinding/dealerBinding"
import { uploadImg } from "@/api/system/upload/upload"
import searchTable from '../../../components/searchTable.vue'
import { scrollTo } from '@/utils/scroll-to'
export default {
  name: "orderAdd",
  components: {
    searchTable
  },
  data(){
    return {
      form: {
        orderCode: '', // 订单编码
        businessDate: '', // 业务日期
        businessModel: '现销', // 业务模式
        orderType: '普通', // 订单类型
        customerType: '1', // 客户类型 1企业客户 2会员客户 3内部客户 4平台客户 5经销商
        customerId: '', // 客户id
        customerName: '', // 客户名称
        customerDept: '', // 客户部门
        miningBySales: '0', // 是否以销定采 0否 1是
        dropShipping: '', // 是否直运 0否 1是
        dropShippingAddress: '', // 直运地址
        shipDate: '', // 发货日期
        contactName: '', // 联系人
        contactTel: '', // 联系电话
        advanceReceiptMoney: '', // 预收金额
        paymentRemarks: '', // 款项备注
        // shippingMethod: '德邦到付', // 运输方式
        shipmentNumber: '', // 物流单号
        receiverName: '', // 收货人
        receiverAddress: '', // 收货地址
        paymentTerms: '', // 付款条款
        credits: '', // 信用额度
        salesDept: '', // 销售部门
        remark: '', // 备注
        salesOrderDetailList: [], // 商品数据
        taxRate: '', // 税率
        shareFileList: [], // 附件
      },
      // 业务模式
      modelList: [
        {
          value: '现销'
        },
        {
          value: '赊销'
        }
      ],
      // 运输方式
      methodList: [
        {
          value: '德邦到付'
        },
        {
          value: '物特现付'
        },
        {
          value: '申通现付'
        },
        {
          value: '送货上门'
        },
        {
          value: '现付'
        }
      ],
      // 发票类型
      invoiceTypeList: [
        {
          value: '普通发票'
        },
        {
          value: '增值税发票'
        },
        {
          value: '收据'
        }
      ],
      // 仓库
      warehouseList: [],
      // 客户
      userArr: [],
      // 日期控制
      pickerOptions: {
        // disabledDate(time) {
        //   return time.getTime() < Date.now() - 24 * 60 * 60 * 1000
        // }
      },
      // 表头
      columns: [
      //   {
      //   prop: "warehouse",
      //   label: "仓库",
      //   width: 120,
      //   type: '4'
      // },
      {
        prop: "productBasicCode",
        label: "产品编码",
        width: 220,
        type: '1'
      },{
        prop: "productCode",
        label: "SKU编码",
        width: 200,
        type: '3'
      }, {
        prop: "productBasicName",
        label: "产品名称",
        width: 200,
        type: '3'
      },
      //   {
      //   prop: "productName",
      //   label: "SKU名称",
      //   width: 200,
      //   type: '2'
      // },
        {
        prop: "productSpace",
        label: "规格",
        width: 120,
        type: '3'
      },{
        prop: "unit",
        label: "单位",
        width: 80,
        type: '3'
      },{
        prop: "batchNumber",
        label: "批次号",
        width: 150,
        type: 'batch'
      },{
        prop: "total",
        label: "数量",
        width: 120,
        type: 'num'
      },{
        prop: "unitPrice",
        label: "未税单价",
        width: 100,
        type: 'unitPrice'
      },{
        prop: "taxUnitPrice",
        label: "含税单价",
        width: 100,
        type: 'taxUnitPrice'
      },{
        prop: "totalPrice",
        label: "未税金额",
        width: 120,
        type: '3'
      },{
        prop: "taxRate",
        label: "税率(%)",
        width: 120,
        type: 'taxRate'
      },{
        prop: "taxPrice",
        label: "税额",
        width: 100,
        type: '3'
      },{
        prop: "totalPriceTax",
        label: "价税合计",
        width: 120,
        type: '3'
      },
      // {
      //   prop: "inventory",
      //   label: "库存",
      //   width: 100,
      //   type: '3'
      //},
      // {
      //   prop: "outStore",
      //   label: "出库数量",
      //   width: 100,
      //   type: '3'
      // },
      {
        prop: "remark",
        label: "备注",
        width: 200,
        type: '5'
      },{
        label: "",
        type: 'del',
        width: 80
      }],
      // 表格数据
      productData: [{
        productId: '',
        productCode: '',
        productName: '',
        productSpace: '',
        unit: '',
        unitPrice: '',
        total: '',
        totalPrice: '',
        taxUnitPrice: '',
        taxRate: '',
        taxPrice: '',
        totalPriceTax: '',
        storeId: '',
        batchNumber: '',
        outStore: '',
        inventory: '',
        warehouse: '',
        remark: '',
        enableBatches: ''
      }],
      id: '',
      // 产品数据
      searchProductList: [],
      // 批次号数据
      searchBatchList: [],
      salesDeptList: [], // 部门
      // isMiningBySales: false, // 是否以销订采
      // isDisabled: true,
      isCustomer: true,
      isPlatForm: true,
      isCustomerCompanyList: false,
      memberUser: [], // 会员客户
      fileList: [],
      isBatch: true,
      customerDeptList: [],
      customerCompanyList: [],
      open: false,
      type: '',
      title: '',
      label: '',
      loading: true,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: undefined,
        phone: undefined
      },
      // 弹出数据列表
      dataList: [],
      searchTableQueryParams: {
        pageNum: 1,
        pageSize: 5,
        searchValue: undefined,
        phone: undefined
      },
      searchCustomerColumns: [],
      searchCustomerData: '',
      searchCustomerShow: false,
      searchCustomerTotal: 0,
      searchProductColumns: [],
      searchProductData: '',
      searchProductShow: false,
      searchProductTotal: 0,
      searchTableProductQueryParams: {
        pageNum: 1,
        pageSize: 5,
        searchValue: undefined,
      },
      productIndex: '',
      dealerBindingList: []
    }
  },
  created() {
    let userInfo = this.$store.state.user.users
    this.form.salesDept = userInfo.dept.deptId
    // 部门
    this.salesDeptList.push({
      deptId: userInfo.dept.deptId,
      deptName: userInfo.dept.deptName
    })
    document.addEventListener('click', this.onClearSearchItem);
    document.addEventListener('click', this.onClearSearchProductItem);
  },
  methods: {
    // 获取详情信息
    getDetailData() {
      orderDetail(this.id).then(res => {
        this.form = res.data
        if(res.data.salesOrderDetailList == null){
          this.productData = []
          this.addProduct()
        }else{
          this.productData = res.data.salesOrderDetailList
        }
      })
    },
    // 客户名称搜索
    onInputCustomer(e){
      if(this.form.customerType == '1'){
        this.searchTableQueryParams.searchValue = this.form.customerName
        this.queryParams.searchValue = e
        this.searchTableQueryParams.status = 1
        this.searchTableQueryParams.approvalStatus = 2
        listUser(this.addDateRange(this.searchTableQueryParams)).then(res => {
          this.searchCustomerShow = true
          this.searchCustomerColumns = []
          this.searchCustomerColumns.push({
            prop: "code",
            label: "编码",
          },{
            prop: "name",
            label: "名称",
          },{
            prop: "shippingAddress",
            label: "详细地址",
            width: 200
          })
          this.searchCustomerData = res.rows
          this.searchCustomerTotal = res.total
        })
      }else if(this.form.customerType == '2'){
        this.searchTableQueryParams.phone = this.form.customerName
        this.queryParams.phone = e
        this.searchTableQueryParams.status = 2
        memberList(this.addDateRange(this.searchTableQueryParams)).then(res => {
          this.searchCustomerShow = true
          this.searchCustomerColumns = []
          this.searchCustomerColumns.push({
            prop: "phone",
            label: "手机号",
          },{
            prop: "nickName",
            label: "姓名",
          })
          this.searchCustomerData = res.rows
          this.searchCustomerTotal = res.total
        })
      }
    },
    // 清除客户
    onClearSearchItem(e){
      const customerInput = document.querySelector('.customerInput')
      const searchItem = document.querySelector('.searchItem')
      if (searchItem && !searchItem.contains(e.target) && customerInput && !customerInput.contains(e.target)) {
        this.searchCustomerShow = false
        this.searchCustomerColumns = []
        this.searchCustomerData = ''
        this.searchCustomerTotal = 0
        this.form.customerId = ''
        this.form.customerName = ''
        this.form.contactName = ''
        this.form.contactTel = ''
        this.form.receiverName = ''
        this.form.receiverAddress = ''
        this.form.paymentTerms = ''
        this.form.taxRate = ''
        this.productData = []
        this.addProduct()
      }
    },
    // 选中客户
    onSearchData(row){
      // console.log(row)
      if(this.form.customerType == '1'){
        this.form.customerId = ''
        this.form.customerName = ''
        this.form.contactName = ''
        this.form.contactTel = ''
        this.form.receiverName = ''
        this.form.receiverAddress = ''
        this.form.paymentTerms = ''
        this.form.taxRate = ''
        detailUser(row.id).then(res => {
          this.form.customerId = res.data.id
          this.form.customerName = res.data.name
          this.form.contactName = res.data.contact
          this.form.contactTel = res.data.contactNumber
          this.form.receiverName = res.data.consignee
          this.form.receiverAddress = res.data.province + res.data.city + res.data.region + res.data.shippingAddress
          this.form.paymentTerms = res.data.paymentTerms
          this.form.taxRate = res.data.customerInformationInvoice == null ? '' : res.data.customerInformationInvoice.taxpayerScale == '0' ? '13' : res.data.customerInformationInvoice.taxpayerScale == '1' ? '9' : res.data.customerInformationInvoice.taxpayerScale == '2' ? '6' : res.data.customerInformationInvoice.taxpayerScale == '3' ? '3' : res.data.customerInformationInvoice.taxpayerScale == '4' ? '1' : ''
          this.form.dropShippingAddress = this.form.receiverAddress
        })
      }else if(this.form.customerType == '2'){
        this.form.customerId = ''
        this.form.customerName = ''
        this.form.contactName = ''
        this.form.contactTel = ''
        this.form.receiverName = ''
        this.form.receiverAddress = ''
        this.form.paymentTerms = ''
        this.form.taxRate = ''
        memberDetail(row.memberInformationId).then(res => {
          this.form.customerId = res.data.memberInformationId
          this.form.customerName = res.data.nickName
          this.form.contactName = res.data.nickName
          this.form.contactTel = res.data.phone
          this.form.receiverName = res.data.memberAddressList.length > 0 ? res.data.memberAddressList[0].name : ''
          this.form.receiverAddress = res.data.memberAddressList.length > 0 ? res.data.memberAddressList[0].province + res.data.memberAddressList[0].city + res.data.memberAddressList[0].area + res.data.memberAddressList[0].address : ''
          this.form.dropShippingAddress = this.form.receiverAddress
        })
      }
      this.searchCustomerShow = false
      this.searchCustomerColumns = []
      this.queryParams.searchValue = ''
      this.queryParams.phone = ''
    },
    // 客户数据显示
    onCustomerShow(){
      if(this.form.customerType == '1'){
        this.open = true
        this.title = '企业客户'
        this.label = '搜索'
        this.type = '1'
        this.dataList = []
        this.getList()
      }else{
        this.open = true
        this.title = '会员客户'
        this.label = '搜索'
        this.type = '3'
        this.dataList = []
        this.getList()
      }
    },
    // 产品名称搜索
    onInputProduct(index,e){
      // console.log(index,e)
      if(e.productId){
        e.productBasicId = ''
        // e.productBasicCode = ''
        e.productBasicName = ''
        e.productId = ''
        e.productCode = ''
        e.productName = ''
        e.batchNumber = ''
        e.productSpace = ''
        e.priceType = ''
        e.unit = ''
        e.taxRate = ''
        e.unitPrice = ''
        e.taxUnitPrice = ''
        e.enableBatches = ''
      }
      this.productIndex = index
      if(this.form.customerName !== '' && this.form.customerId !== ''){
        this.searchTableProductQueryParams.searchValue = e.productBasicCode
        this.queryParams.searchValue = e.productBasicCode
        listSalesProduct(this.addDateRange(this.searchTableProductQueryParams)).then(res => {
          this.searchProductShow = true
          this.searchProductColumns = []
          this.searchProductColumns.push({
            prop: "productCode",
            label: "编码",
          },{
            prop: "productName",
            label: "名称",
          },{
            prop: "productSpace",
            label: "规格",
          },{
            prop: "batchNumber",
            label: "批次",
          })
          this.searchProductData = res.rows
          this.searchProductTotal = res.total
          this.handleScrollBottom()

        })
      }else{
        this.$modal.msgError('请选择客户！')
        this.productData = []
        this.addProduct()
      }
    },
    onPageProduct(){
      listSalesProduct(this.addDateRange(this.searchTableProductQueryParams)).then(res => {
        this.searchProductShow = true
        this.searchProductData = res.rows
        this.searchProductTotal = res.total
        this.handleScrollBottom()
      })
    },
    // 清除产品
    onClearSearchProductItem(e){
      const productInput = document.querySelector('.productInput')
      const searchProduct = document.querySelector('.searchProduct')
      if(searchProduct && !searchProduct.contains(e.target) && productInput && !productInput.contains(e.target)){
        this.searchProductShow = false
        this.searchProductColumns = []
        this.searchProductData = ''
        this.searchProductTotal = 0
        this.productData.splice(this.productIndex, 1)
        if(this.productData.length == 0){
          this.addProduct()
        }
        // this.productData = []
        // this.addProduct()
      }
    },
    // 产品选择
    onSearchProductData(row){
      // console.log(row)
      // 产品选择
      let price = row.sellingPrice !== null ? row.sellingPrice : 0
      let taxUnitPrice = ''
      let unitPrice = ''
      let total = ''
      let totalPrice = ''
      let taxPrice = ''
      let totalPriceTax = ''
      let obj = ''
      if(row.priceType == '1'){
        taxUnitPrice = price // 含税单价
        unitPrice = (price / (1 + (Number(this.form.taxRate) / 100))).toFixed(2) // 未税单价
      }else{
        taxUnitPrice = (price * (1 + (Number(this.form.taxRate) / 100))).toFixed(2) // 含税单价
        unitPrice = price // 未税单价
      }
      if(this.form.customerType == '5'){
        total = row.storeNumber
        totalPrice = (total * unitPrice).toFixed(2) // 金额
        totalPriceTax = (total * taxUnitPrice).toFixed(2) // 价税合计
        taxPrice =  ((totalPriceTax / (1 + (Number(this.form.taxRate) / 100))) * (Number(this.form.taxRate) / 100)).toFixed(2) // 税额
      }else{
        total = ''
        totalPrice = ''
        totalPriceTax = ''
        taxPrice = ''
      }
      obj = {
        productBasicId: row.productId, // 产品Id
        productBasicCode: row.productCode, // 产品编码
        productBasicName: row.productName, // 产品名称
        productId: row.specificationId, // sku产品Id
        productCode: row.specificationCode, // sku产品编码
        productName: row.specificationName, // sku产品名称
        batchNumber: row.batchNumber, // 批号
        productSpace: row.value1 + row.value2, // 规格
        total: total, // 数量
        priceType: row.priceType, // 价格类型
        unit: row.unit, // 单位
        taxRate: this.form.taxRate, // 税率
        unitPrice: unitPrice, // 未税单价
        taxUnitPrice: taxUnitPrice, // 含税单价
        totalPrice: totalPrice, // 金额
        taxPrice: taxPrice, // 税款
        totalPriceTax: totalPriceTax, // 价税合计
        enableBatches: row.enableBatches, // 是否启用批次
        remark: '', // 备注
      }
      if(this.productData.length == 0){
        this.productData.push(obj)
      }else{
        let index = this.productData.findIndex(item => item.productCode == row.specificationCode && item.batchNumber == row.batchNumber)
        if(index > -1){
          this.$modal.msgError('产品已存在，请重新选择！')
        }else{
          this.productData.push(obj)
        }
      }
      let arr = this.productData.filter(item => item.productCode !== '')
      this.productData = arr
      this.addProduct()
      this.searchProductShow = false
      this.searchProductColumns = []
      this.queryParams.searchValue = ''
    },
    handleScrollBottom() {
      this.$nextTick(() => {
        scrollTo(1000, 800)
      })
    },
    // 产品数据显示
    productShow(){
      if(this.form.customerName !== '' && this.form.customerId !== ''){
        this.open = true
        this.title = '产品'
        this.label = '搜索'
        this.type = '2'
        this.dataList = []
        this.getList()
      }else{
        this.$modal.msgError('请选择客户！')
      }
    },
    // 获取弹窗数据
    getList(){
      if(this.type == '1'){
        this.loading = true
        this.queryParams.status = 1
        this.queryParams.approvalStatus = 2
        listUser(this.addDateRange(this.queryParams)).then(res => {
          this.dataList = res.rows
          this.total = res.total
          this.loading = false
        })
      }else if(this.type == '2'){
        this.loading = true
        // this.queryParams.approvalStatus = 2
        // this.queryParams.productStatus = 1
        listSalesProduct(this.addDateRange(this.queryParams)).then(res => {
          this.dataList = res.rows
          this.total = res.total
          this.loading = false
        })
      }else if(this.type == '3'){
        this.loading = true
        this.queryParams.status = 2
        memberList(this.addDateRange(this.queryParams)).then(res => {
          this.dataList = res.rows
          this.total = res.total
          this.loading = false
        })
      }
    },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置按钮
    resetQuery() {
      this.queryParams.searchValue = ''
      this.queryParams.phone = ''
      this.handleQuery()
    },
    // 选中数据
    onSelectData(row){
      if(this.type == '1'){
        this.form.customerId = ''
        this.form.customerName = ''
        this.form.contactName = ''
        this.form.contactTel = ''
        this.form.receiverName = ''
        this.form.receiverAddress = ''
        this.form.paymentTerms = ''
        this.form.taxRate = ''
        detailUser(row.id).then(res => {
          this.form.customerId = res.data.id
          this.form.customerName = res.data.name
          this.form.contactName = res.data.contact
          this.form.contactTel = res.data.contactNumber
          this.form.receiverName = res.data.consignee
          this.form.receiverAddress = res.data.province + res.data.city + res.data.region + res.data.shippingAddress
          this.form.paymentTerms = res.data.paymentTerms
          this.form.taxRate = res.data.customerInformationInvoice == null ? '' : res.data.customerInformationInvoice.taxpayerScale == '0' ? '13' : res.data.customerInformationInvoice.taxpayerScale == '1' ? '9' : res.data.customerInformationInvoice.taxpayerScale == '2' ? '6' : res.data.customerInformationInvoice.taxpayerScale == '3' ? '3' : res.data.customerInformationInvoice.taxpayerScale == '4' ? '1' : ''
          this.form.dropShippingAddress = this.form.receiverAddress
        })
      }else if(this.type == '2'){
        // 产品选择
        let price = row.sellingPrice !== null ? row.sellingPrice : 0
        let taxUnitPrice = ''
        let unitPrice = ''
        let total = ''
        let totalPrice = ''
        let totalPriceTax = ''
        let taxPrice = ''
        let obj = ''
        if(row.priceType == '1'){
          taxUnitPrice = price // 含税单价
          unitPrice = (price / (1 + (Number(this.form.taxRate) / 100))).toFixed(2) // 未税单价
        }else{
          taxUnitPrice = (price * (1 + (Number(this.form.taxRate) / 100))).toFixed(2) // 含税单价
          unitPrice = price // 未税单价
        }
        if(this.form.customerType == '5'){
          total = row.storeNumber
          totalPrice = (total * unitPrice).toFixed(2) // 金额
          totalPriceTax = (total * taxUnitPrice).toFixed(2) // 价税合计
          taxPrice =  ((totalPriceTax / (1 + (Number(this.form.taxRate) / 100))) * (Number(this.form.taxRate) / 100)).toFixed(2) // 税额
        }else{
          total = ''
          totalPrice = ''
          totalPriceTax = ''
          taxPrice = ''
        }
        obj = {
          productBasicId: row.productId, // 产品Id
          productBasicCode: row.productCode, // 产品编码
          productBasicName: row.productName, // 产品名称
          productId: row.specificationId, // sku产品Id
          productCode: row.specificationCode, // sku产品编码
          productName: row.specificationName, // sku产品名称
          batchNumber: row.batchNumber, // 批号
          productSpace: row.value1 + row.value2, // 规格
          total: total, // 数量
          priceType: row.priceType, // 价格类型
          unit: row.unit, // 单位
          taxRate: this.form.taxRate, // 税率
          unitPrice: unitPrice, // 未税单价
          taxUnitPrice: taxUnitPrice, // 含税单价
          totalPrice: totalPrice, // 金额
          taxPrice: taxPrice, // 税款
          totalPriceTax: totalPriceTax, // 价税合计
          enableBatches: row.enableBatches, // 是否启用批次
          remark: '', // 备注
        }
        if(this.productData.length == 0){
          this.productData.push(obj)
        }else{
          let index = this.productData.findIndex(item => item.productCode == row.specificationCode && item.batchNumber == row.batchNumber)
          if(index > -1){
            this.$modal.msgError('产品已存在，请重新选择！')
          }else{
            this.productData.push(obj)
          }
        }
        let arr = this.productData.filter(item => item.productCode !== '')
        this.productData = arr
        this.addProduct()
      }else if(this.type == '3'){
        this.form.customerId = ''
        this.form.customerName = ''
        this.form.contactName = ''
        this.form.contactTel = ''
        this.form.receiverName = ''
        this.form.receiverAddress = ''
        this.form.paymentTerms = ''
        this.form.taxRate = ''
        memberDetail(row.memberInformationId).then(res => {
          this.form.customerId = res.data.memberInformationId
          this.form.customerName = res.data.nickName
          this.form.contactName = res.data.nickName
          this.form.contactTel = res.data.phone
          this.form.receiverName = res.data.memberAddressList.length > 0 ? res.data.memberAddressList[0].name : ''
          this.form.receiverAddress = res.data.memberAddressList.length > 0 ? res.data.memberAddressList[0].province + res.data.memberAddressList[0].city + res.data.memberAddressList[0].area + res.data.memberAddressList[0].address : ''
          this.form.dropShippingAddress = this.form.receiverAddress
        })
      }
      this.open = false
      this.title = ''
      this.label = ''
      this.type = ''
      this.queryParams.searchValue = undefined
      this.queryParams.phone = undefined
      this.handleQuery()
      this.dataList = []
    },
    // 关闭弹窗
    handleClose(){
      this.open = false
      this.title = ''
      this.label = ''
      this.type = ''
      this.queryParams.searchValue = undefined
      this.queryParams.phone = undefined
      this.handleQuery()
      this.dataList = []
    },
    // 数量验证
    inputNum(val, row){
      if(val == '' || val == undefined){
        this.$modal.msgError('数量不能为空!')
        row.total = ''
        row.totalPrice = ''
        row.totalPriceTax = ''
        row.taxPrice = ''
      }else{
        if(/^[1-9]\d*$|^0$/.test(val) == false){
          this.$modal.msgError('数量必须是数字！')
          return false
        }else{
          if(JSON.parse(val) <= 0){
            row.total = ''
            this.$modal.msgError('数量必须大于0!')
          }else{
            if(this.form.miningBySales == '0' || this.form.miningBySales == ''){
              row.totalPrice = (val * row.unitPrice).toFixed(2) // 金额
              row.totalPriceTax = (val * row.taxUnitPrice).toFixed(2) // 价税合计
              row.taxPrice =  ((row.totalPriceTax / (1 + (Number(row.taxRate) / 100))) * (Number(row.taxRate) / 100)).toFixed(2) // 税额
            }else{
              row.totalPrice = (val * row.unitPrice).toFixed(2) // 金额
              row.totalPriceTax = (val * row.taxUnitPrice).toFixed(2) // 价税合计
              row.taxPrice =  ((row.totalPriceTax / (1 + (Number(row.taxRate) / 100))) * (Number(row.taxRate) / 100)).toFixed(2) // 税额
            }
          }
        }
      }
    },
    // 未税单价
    inputUnitPrice(val, row){
      if(val == '' || val == undefined){
        this.$modal.msgError('未税单价不能为空!')
        row.unitPrice = ''
        row.taxUnitPrice = ''
        row.totalPrice = ''
        row.totalPriceTax = ''
        row.taxPrice = ''
      }else{
        if(/^(([1-9]{1}\d*)|(0{1}))((.\d{0,2})?)$/.test(val) == false){
          this.$modal.msgError('请输入正确的未税单价！')
          row.unitPrice = ''
          row.taxUnitPrice = ''
          row.totalPrice = ''
          row.totalPriceTax = ''
          row.taxPrice = ''
          return false
        }else {
          row.taxUnitPrice = (row.unitPrice * (1 + (Number(row.taxRate) / 100))).toFixed(2)
          if(row.total !== ''){
            row.totalPrice = (val * row.total).toFixed(2) // 未税金额
            row.totalPriceTax = (row.total * row.taxUnitPrice).toFixed(2) // 价税合计
            row.taxPrice = (row.totalPriceTax - row.totalPrice).toFixed(2) // 税额
          }
        }
      }
    },
    // 含税单价
    inputTaxUnitPrice(val, row){
      if(val == '' || val == undefined){
        this.$modal.msgError('含税单价不能为空!')
        row.unitPrice = ''
        row.taxUnitPrice = ''
        row.totalPrice = ''
        row.totalPriceTax = ''
        row.taxPrice = ''
      }else{
        if(/^(([1-9]{1}\d*)|(0{1}))((.\d{0,2})?)$/.test(val) == false){
          this.$modal.msgError('请输入正确的含税单价！')
          row.unitPrice = ''
          row.taxUnitPrice = ''
          row.totalPrice = ''
          row.totalPriceTax = ''
          row.taxPrice = ''
          return false
        }else {
          row.unitPrice = (Number(val) / (1 + (Number(row.taxRate) / 100))).toFixed(2) // 未税单价
          if(row.total !== ''){
            row.totalPrice = (row.unitPrice * row.total).toFixed(2) // 未税金额
            row.totalPriceTax = (row.total * row.taxUnitPrice).toFixed(2) // 价税合计
            row.taxPrice = (row.totalPriceTax - row.totalPrice).toFixed(2) // 税额
          }
        }
      }
    },
    // 数量判断库存
    inputBlur(val, row){
      if(this.form.miningBySales == '0' || this.form.miningBySales == ''){
        if(val.target.value !== ''){
          let arr = this.productData.filter(item => item.productCode !== '')
          this.productData = arr
          this.addProduct()
        }
      }else{
        this.addProduct()
      }
    },
    // 税率
    inputTaxRate(row){
      // 判断价格类型 1 含税价格 0 未税价格
      if(row.priceType == '0'){
        row.taxUnitPrice = (row.unitPrice * (1 + (Number(row.taxRate) / 100))).toFixed(2) // 含税单价
      }else{
        row.unitPrice = (Number(row.taxUnitPrice) / (1 + (Number(row.taxRate) / 100))).toFixed(2) // 未税单价
      }
      if(row.total !== undefined){
        row.totalPrice = (row.unitPrice * row.total).toFixed(2) // 未税金额
        row.totalPriceTax = (row.total * row.taxUnitPrice).toFixed(2) // 价税合计
        row.taxPrice = (row.totalPriceTax - row.totalPrice).toFixed(2) // 税额
      }else{
        row.totalPrice = ''
        row.totalPriceTax = ''
        row.taxPrice = ''
      }
      // row.total = ''
      let arr = this.productData.filter(item => item.productCode !== '')
      this.productData = arr
      this.addProduct()
    },
    // 添加一条产品信息
    addProduct(){
      let data = {
        key: {
          orderId: this.id ? this.id : '',
          productId: ''
        }
      }
      this.columns.forEach(item =>{
        data.key[item.prop] = ''
      })
      this.productData.push(JSON.parse(JSON.stringify(data.key)))
    },
    // 删除数据
    deleteRow(index, item){
      // console.log(item)
      if(item.productId !== ''){
        this.$modal.confirm('是否确认删除当前数据？', '删除').then(() => {
          if(item.id && item.id !== '' || item.id !== undefined){
            delProduct(item.id).then(res => {
              // console.log(res)
              if(res.code == 200){
                this.productData.splice(index, 1)
                if(this.productData.length == 0){
                  this.addProduct()
                }
              }
            })
          }else{
            this.productData.splice(index, 1)
            if(this.productData.length == 0){
              this.addProduct()
            }
          }
        }).catch(() => {})
      }else{
        this.$modal.msgError('最少保留一条可编辑数据！')
      }
    },
    // 客户类型
    onCustomerType(val){
      if(this.productData.length > 0){
        // this.$modal.msgError('客户类型已更改，请重新选择产品！')
        this.productData = []
        this.addProduct()
      }
      this.form.customerId = ''
      this.form.customerName = ''
      this.form.contactName = ''
      this.form.contactTel = ''
      this.form.receiverName = ''
      this.form.receiverAddress = ''
      this.form.paymentTerms = ''
      this.form.taxRate = ''
      if(val == '1'){
        // this.getListUser()
        // this.isCustomer = true
        this.isPlatForm = true
        this.isCustomerCompanyList = false
        this.customerCompanyList = []
        this.form.customerDept = ''
      }else if(val == '2'){
        // this.getMemberUser()
        // this.isCustomer = false
        this.isPlatForm = true
        this.isCustomerCompanyList = false
        this.customerCompanyList = []
        this.form.customerDept = ''
      }else if(val == '3'){
        this.isPlatForm = false
        this.getDeptTree()
      }else if(val == '5'){
        this.isPlatForm = false
        this.getDealerBindingList()
      }
    },
    // 租户绑定列表
    getDealerBindingList(){
      list().then(res => {
        this.dealerBindingList = res.rows
      })
    },
    // 选择租户
    onDealerBinding(id){
      let data = this.dealerBindingList.find(item => item.id == id)
      this.form.customerId = data.id
      this.form.customerName = data.corporateName
      this.form.contactName = data.contacts // 联系人
      this.form.contactTel = data.phone // 联系电话
      this.form.receiverName = data.corporateName // 收货人
      this.form.receiverAddress = data.address
    },
    // 上传附件
    onUploadType(item){
      this.$modal.loading("正在上传")
      let formData = new FormData()
      let file = item.raw
      formData.append('file', file)
      uploadImg(formData).then(res => {
        this.$modal.closeLoading()
        this.$modal.msgSuccess("上传成功")
        let obj = {
          name: res.data.fileName,
          url: res.data.fileUrl
        }
        this.fileList.push(obj)
        this.form.shareFileList.push(res.data)
      })
    },
    handleRemove(item) {
      const a = this.fileList.findIndex(i => i.name == item.name)
      this.fileList.splice(a, 1)
      const idx = this.form.shareFileList.findIndex(i => i.fileName == item.name)
      this.form.shareFileList.splice(idx, 1)
    },
    onExceed() {
      this.$modal.msgError('图片上传不能超过9张!')
    },
    beforeRemove(file) {
      return this.$confirm(`确定移除 ${ file.name }？`)
    },
    // 获取组织架构
    getDeptTree(){
      deptTree().then(res => {
        let arr = []
        res.data.map(item => {
          if(item.id == 0){
            arr.push(Object.assign(
              {},item,{ disabled: true }
            ))
          }else{
            arr.push(item)
          }
        })
        this.customerDeptList = arr
        if(this.form.customerType == '3'){
          this.isCustomer = true
          this.isPlatForm = false
          if(this.form.customerDept !== null && this.form.customerDept !== ''){
            this.isCustomerCompanyList = true
            let data = this.customerDeptList.find(item => item.id == this.form.customerId)
            this.customerCompanyList = data.deptList
          }
        }
      })
    },
    onPlatForm(id){
      let data = this.customerDeptList.find(item => item.id == id)
      this.form.customerId = data.id
      this.form.customerName = data.orgName
      this.form.contactName = data.leader // 联系人
      this.form.contactTel = data.leaderPhone // 联系电话
      this.form.receiverName = data.contact // 收货人
      this.form.receiverAddress = data.province + data.city + data.region + data.contactAddress // 收货地址
      if(data.deptList !== null){
        this.isCustomerCompanyList = true
        this.customerCompanyList = data.deptList
        this.form.customerDept = ''
      }else{
        this.isCustomerCompanyList = false
        this.customerCompanyList = []
        this.form.customerDept = ''
      }
    },
    // 提交数据
    submitForm(e) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 过滤掉空数据
          let arr = this.productData.filter(item => item.productCode !== '')
          this.form.salesOrderDetailList = arr
          let total = arr.every(val => !!val.total)
          let warehouse = arr.every(val => !!val.warehouse)
          let unitPrice = arr.every(val => !!val.unitPrice)
          // console.log(arr)
          // 是否以销订采 0否 1是
          if(this.form.miningBySales == '0' || this.form.miningBySales == ''){
            if(e == 1){
              if(this.id){
                let arr = this.form
                arr.id = this.id
                this.$modal.loading("正在保存")
                orderEdit(arr).then(res=> {
                  if(res.code == 200){
                    this.getDetailData()
                    this.$modal.msgSuccess("保存成功")
                    this.$modal.closeLoading()
                  }
                })
              }else{
                this.$modal.loading("正在保存")
                orderAdd(this.form).then(res=> {
                  // console.log(res)
                  if(res.code == 200){
                    this.id = res.data.id
                    if(res.data.taxRate){
                      this.form.taxRate = res.data.taxRate
                    }
                    this.form.orderCode = res.data.orderCode
                    if(res.data.salesOrderDetailList.length > 0){
                      this.productData = res.data.salesOrderDetailList
                    }else{
                      this.productData = []
                      this.addProduct()
                    }
                    this.$modal.msgSuccess("保存成功")
                    this.$modal.closeLoading()
                  }
                })
              }
            }else{
              if(arr.length > 0){
                // 判断数量是否为空
                if(total == true){
                  // 判断单价不能为空
                  if(unitPrice == true){
                    if(this.id){
                      // console.log(this.form)
                      let arr = this.form
                      arr.id = this.id
                      arr.approveStatus = 0
                      arr.status = 1
                      this.$modal.loading("正在提交")
                      orderEdit(arr).then(res=> {
                        if(res.code == 200){
                          this.$refs.form.resetFields()
                          this.$tab.closeOpenPage({ path: "/sales/salesOrder/list" })
                          this.$modal.msgSuccess("新建成功")
                          this.$modal.closeLoading()
                        }
                      })
                    }else{
                      let arr = this.form
                      arr.approveStatus = 0
                      arr.status = 1
                      this.$modal.loading("正在提交")
                      orderAdd(arr).then(res=> {
                        if(res.code == 200){
                          this.$refs.form.resetFields()
                          this.$tab.closeOpenPage({ path: "/sales/salesOrder/list" })
                          this.$modal.msgSuccess("新建成功")
                          this.$modal.closeLoading()
                        }
                      })
                    }
                  }else{
                    this.$modal.msgError('单价不能为空！')
                  }
                }else{
                  this.$modal.msgError('数量不能为空！')
                }
              }else{
                this.$modal.msgError('请选择商品！')
              }
            }
          }else{
            if(e == 1){
              if(this.id){
                let arr = this.form
                arr.id = this.id
                this.$modal.loading("正在保存")
                orderEdit(arr).then(res=> {
                  if(res.code == 200){
                    this.getDetailData()
                    this.$modal.msgSuccess("保存成功")
                    this.$modal.closeLoading()
                  }
                })
              }else{
                this.$modal.loading("正在保存")
                orderAdd(this.form).then(res=> {
                  // console.log(res)
                  if(res.code == 200){
                    this.id = res.data.id
                    if(res.data.taxRate){
                      this.form.taxRate = res.data.taxRate
                    }
                    this.form.orderCode = res.data.orderCode
                    this.productData = res.data.salesOrderDetailList
                    this.addProduct()
                    this.$modal.msgSuccess("保存成功")
                    this.$modal.closeLoading()
                  }
                })
              }
            }else{
              if(arr.length > 0){
                // 判断数量是否为空
                if(total == true){
                  // 判断单价不能为空
                  if(unitPrice == true){
                    if(warehouse == true){
                      if(this.id){
                        let arr = this.form
                        arr.id = this.id
                        arr.approveStatus = 0
                        arr.status = 1
                        this.$modal.loading("正在提交")
                        orderEdit(arr).then(res=> {
                          if(res.code == 200){
                            this.$refs.form.resetFields()
                            this.$tab.closeOpenPage({ path: "/sales/salesOrder/list" })
                            this.$modal.msgSuccess("新建成功")
                            this.$modal.closeLoading()
                          }
                        })
                      }else{
                        let arr = this.form
                        arr.approveStatus = 0
                        arr.status = 1
                        this.$modal.loading("正在提交")
                        orderAdd(arr).then(res=> {
                          if(res.code == 200){
                            this.$refs.form.resetFields()
                            this.$tab.closeOpenPage({ path: "/sales/salesOrder/list" })
                            this.$modal.msgSuccess("新建成功")
                            this.$modal.closeLoading()
                          }
                        })
                      }
                    }else{
                      this.$modal.msgError('仓库不能为空！')
                    }
                  }else{
                    this.$modal.msgError('单价不能为空！')
                  }
                }else{
                  this.$modal.msgError('数量不能为空！')
                }
              }else{
                this.$modal.msgError('请选择商品！')
              }
            }
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.del{
  color: #ff0000;
  cursor: pointer;
}
.searchItem{
  position:absolute;
  width:200%;
  z-index:2;
}
.searchProduct{
  position:relative;
  width:500px;
  z-index:2;
  top:-1px;
}
::v-deep .searchItem .el-table th{
  padding:0;
}
.searchTable{
  padding:5px 0 35px !important;
  margin:0!important;
  border: 1px solid #dfe6ec;
  border-top:none;
}
</style>
